package com.cq.hd.base;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: mYunYu
 *  2021/7/9 10:36
 * @version: v1.0
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    private AppLoginUserMethodArgumentResolver appLoginUserMethodArgumentResolver;

    @Autowired
    public void setLoginUserMethodArgumentResolver(AppLoginUserMethodArgumentResolver appLoginUserMethodArgumentResolver) {
        this.appLoginUserMethodArgumentResolver = appLoginUserMethodArgumentResolver;
    }

    private MerchantLoginUserMethodArgumentResolver merchantLoginUserMethodArgumentResolver;

    @Autowired
    public void setMerchantLoginUserMethodArgumentResolver(MerchantLoginUserMethodArgumentResolver merchantLoginUserMethodArgumentResolver) {
        this.merchantLoginUserMethodArgumentResolver = merchantLoginUserMethodArgumentResolver;
    }

    @Override
    protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(appLoginUserMethodArgumentResolver);
        argumentResolvers.add(merchantLoginUserMethodArgumentResolver);
    }

    /**
     * @Description: 使用阿里 fastjson 作为 JSON MessageConverter
     * @Param: [converters]
     * @Return: void
     * @Auther: mYunYu
     *  2021/7/9 15:25
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setSerializerFeatures(
                // 保留 Map 空的字段
                SerializerFeature.WriteMapNullValue,
                // 将 String 类型的 null 转成""
                SerializerFeature.WriteNullStringAsEmpty,
                // 将 Number 类型的 null 转成 0
                SerializerFeature.WriteNullNumberAsZero,
                // 将 List 类型的 null 转成 []
                SerializerFeature.WriteNullListAsEmpty,
                // 将 Boolean 类型的 null 转成 false
                SerializerFeature.WriteNullBooleanAsFalse,
                // 避免循环引用
                SerializerFeature.DisableCircularReferenceDetect);

        converter.setFastJsonConfig(config);
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        List<MediaType> mediaTypeList = new ArrayList<>();
        // 解决中文乱码问题，相当于在 Controller 上的 @RequestMapping 中加了个属性 produces = "application/json"
        mediaTypeList.add(MediaType.APPLICATION_JSON);
        converter.setSupportedMediaTypes(mediaTypeList);
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON_UTF8, MediaType.valueOf("application/vnd.spring-boot.actuator.v2+json")));
        converters.add(converter);
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
